class AuthD::Request
	# Reset elements for which keys are present in `new_profile_entries`.
	IPC::JSON.message EditProfileEntries, 7 do
		property user : UserID? = nil

		property new_profile_entries : Hash(String, JSON::Any)

		def initialize(@new_profile_entries, @user = nil)
		end

		def to_s(io : IO)
			super io
			io << " (user: #{@user})"
		end

		def handle(authd : AuthD::Service, fd : Int32)
			logged_user = authd.get_logged_user_full? fd
			return Response::ErrorMustBeAuthenticated.new if logged_user.nil?

			user = if u = @user
				logged_user.assert_permission("authd", "*", User::PermissionLevel::Edit)
				authd.user? u
			else
				logged_user
			end
			return Response::ErrorUserNotFound.new if user.nil?

			new_profile_entries = user.profile || Hash(String, JSON::Any).new

			invalid_profile_keys = Array(String).new
			unless logged_user.admin
				authd.configuration.read_only_profile_keys.each do |key|
					if @new_profile_entries.has_key? key
						invalid_profile_keys << key
					end
				end
			end

			if invalid_profile_keys.size > 0
				return Response::ErrorReadOnlyProfileKeys.new invalid_profile_keys
			end

			@new_profile_entries.each do |key, value|
				new_profile_entries[key] = value
			end

			user.profile = new_profile_entries

			authd.users_per_uid.update user.uid.to_s, user

			Response::User.new user.to_public
		end
	end
	AuthD.requests << EditProfileEntries
end
